package com.thinkgem.jeesite.modules.website;

import com.thinkgem.jeesite.modules.area_info.entity.AreaInfo;
import com.thinkgem.jeesite.modules.guide.entity.Guiding;
import com.thinkgem.jeesite.modules.guide.service.GuidingService;
import com.thinkgem.jeesite.modules.lane.entity.Lane;
import com.thinkgem.jeesite.modules.lane.service.LaneService;
import com.thinkgem.jeesite.modules.signallight.entity.TSignalLight;
import com.thinkgem.jeesite.modules.sys.entity.Dict;
import com.thinkgem.jeesite.modules.sys.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xlp
 * @date 2019/5/16
 */
@RestController
@RequestMapping("/guide")
public class GuideController {

    @Autowired
    private GuidingService guidingService;

    @Autowired
    private LaneService laneService;

    @Autowired
    private DictService dictService;

    /**
     * 获取单个车道信息
     * @param request
     * @param id
     * @return
     */
    @RequestMapping(value = "/getGuide", method = RequestMethod.POST)
    public Guiding getGuide(HttpServletRequest request, String id) {
        Guiding guiding = guidingService.get(id);
        return guiding;
    }

    /**
     * 获取车道信息集合
     * @param request
     * @return
     */
    @RequestMapping(value = "/getLaneList", method = RequestMethod.POST)
    public List<Lane> addOrUpdateGuide(HttpServletRequest request) {
        List<Lane> laneList = laneService.findList(new Lane());
        return laneList;
    }

    /**
     * 新增或修改车道信息
     * @param request
     * @param guiding
     */
    @RequestMapping(value = "/addOrUpdateGuide", method = RequestMethod.POST)
    public String addOrUpdateGuide(HttpServletRequest request, Guiding guiding) {
        guidingService.save(guiding);
        return "";
    }


    /**
     * 获取引导灯信息集合
     * @param request
     * @return
     */
    @RequestMapping(value = "/getGuideLightList", method = RequestMethod.POST)
    public ResponseData getLaneList(HttpServletRequest request) {
        List<Guiding> guidingList = guidingService.findList(new Guiding());
        ResponseData responseData = new ResponseData();
        if (!CollectionUtils.isEmpty(guidingList)){
            for (Guiding guiding:guidingList){

                Lane lane = laneService.get(guiding.getLaneId());
                if (lane!=null){
                    guiding.setLaneName(lane.getLaneName());
                }

                Dict dict = new Dict();
                List<Dict> dictList = new ArrayList<Dict>();
                dict.setType("light_status");
                dict.setValue(guiding.getStatus());
                dictList = dictService.findList(dict);
                if (!CollectionUtils.isEmpty(dictList)){
                    guiding.setStatus(dictList.get(0).getLabel());
                }
            }
            responseData.setCode(200);
            responseData.setMsg("查询成功");
            responseData.setTotal(String.valueOf(guidingList.size()));
            responseData.setData(guidingList);
        }else {
            responseData.setCode(200);
            responseData.setMsg("查询失败");
            responseData.setTotal("0");
            responseData.setData(null);
        }
        return responseData;
    }

    /**
     * 删除/批量删除引导灯信息
     * @param request
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public String delete(HttpServletRequest request, String id) {
        String res = "";
        String[] ids = id.split(",");
        Guiding guiding = new Guiding();
        try {
            for (int i=0; i<ids.length; i++){
                guiding.setId(ids[i]);
                guidingService.delete(guiding);
            }
            res = "200";
        }catch (Exception e){
            e.printStackTrace();
            res = "201";
        }

        return res;
    }
}
